<script setup>
import { getBizCompany, updateBizCompany } from "@/api/biz/bizCompany";

const props = defineProps({
  id: {
    type: [Number, String],
    default: ""
  }
});

const formRef = ref();
const data = ref({
  loading: false,
  form: {
    id: props.id
  },
  rules: {}
});

onMounted(() => {
  if (data.value.form.id != "") {
    getInfo();
  }
});

function getInfo() {
  data.value.loading = true;
  getBizCompany(props.id).then((res) => {
    data.value.loading = false;
    data.value.form = res.data;
  });
}

defineExpose({
  submit(callback) {
    if (data.value.form.id == "") {
      formRef.value.validate((valid) => {
        if (valid) {
          updateBizCompany(data.value.form).then(() => {
            ElMessage.success({
              message: "保存成功",
              center: true
            });
            callback && callback();
          });
        }
      });
    } else {
      formRef.value.validate((valid) => {
        if (valid) {
          updateBizCompany(data.value.form).then(() => {
            ElMessage.success({
              message: "修改成功",
              center: true
            });
            callback && callback();
          });
        }
      });
    }
  }
});
</script>

<template>
  <div v-loading="data.loading">
    <el-form
      ref="formRef"
      :model="data.form"
      :rules="data.rules"
      label-width="120px"
      label-suffix=" "
      label-position="top"
      style="padding: 0 20px"
    >
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="租户名称" prop="companyName">
            <el-input
              placeholder="输入租户名称"
              v-model="data.form.companyName"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系人" prop="lianxiUser">
            <el-input placeholder="输入联系人" v-model="data.form.lianxiUser"
          /></el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="联系电话" prop="lianxiPhone">
            <el-input placeholder="联系电话" v-model="data.form.lianxiPhone" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="联系地址" prop="lianxiAddress">
            <el-input
              placeholder="联系地址"
              v-model="data.form.lianxiAddress"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="短信通知" prop="smsIsopen">
            <el-radio-group v-model="data.form.smsIsopen">
              <el-radio-button label="1">是</el-radio-button>
              <el-radio-button label="0">否</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="OSS存储" prop="ossIsopen">
            <el-radio-group v-model="data.form.ossIsopen">
              <el-radio-button label="1">是</el-radio-button>
              <el-radio-button label="0">否</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="有效时间（年）" prop="youxiaoshij">
            <el-input-number
              placeholder="有效时间（年）"
              v-model="data.form.youxiaoshij"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input
              placeholder="输入参数值"
              v-model="data.form.remark"
              type="textarea"
              :rows="3"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
